package com.mytest;

import android.animation.Animator;
import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.mytest.video.SampleVideo;
import com.mytest.video.SwitchVideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlayButtonActivity extends Activity {

    private TextView tv;
    private boolean isOpen = false;
    String url = "http://xmwjtest.oss-cn-shenzhen.aliyuncs.com/MagicYinSound_o7RSnwtOHaFeDLvEu14UVV-zeegA_1497085237000.m4a";

    @BindView(R.id.video_player)
    SampleVideo videoPlayer;
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    //倒计时
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int countDownTime = (videoPlayer.getDuration() - videoPlayer.getCurrentPositionWhenPlaying()) / 1000;
            tv.setText("\"" + countDownTime + "\"" + "     ■");
            mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_button);
        ButterKnife.bind(this);
        init();
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                setScale(rl);
                if (isOpen) {
                    performAnimate(tv, 220, 100);
                    videoPlayer.onVideoPause();
                    mHandler.removeCallbacks(runnable);
                } else {
                    performAnimate(tv, 100, 220);
                    videoPlayer.startPlayLogic();
                    mHandler.postDelayed(runnable, 1000);
                }
                isOpen = !isOpen;
            }
        });
    }

    //textview变形动画
    private void performAnimate(final View target, final int start, final int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            //持有一个IntEvaluator对象，方便下面估值的时候使用
            private IntEvaluator mEvaluator = new IntEvaluator();

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                //获得当前动画的进度值，整型，1-100之间
                int currentValue = (Integer) animator.getAnimatedValue();

                //计算当前进度占整个动画过程的比例，浮点型，0-1之间
                float fraction = currentValue / 100f;

                //这里我偷懒了，不过有现成的干吗不用呢
                //直接调用整型估值器通过比例计算出宽度，然后再设给Button
                target.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end);
                target.requestLayout();
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                tv.setText("");
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (isOpen) {
                    tv.setText("\"" + videoPlayer.getDuration() / 1000 + "\"" + "     ■");
                } else {
                    tv.setText("►");
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.setDuration(150).start();
    }

    private void init() {
        String url = "http://xmwjtest.oss-cn-shenzhen.aliyuncs.com/MagicYinSound_o7RSnwtOHaFeDLvEu14UVV-zeegA_1497085237000.m4a";

        //视频1
        String source1 = "http://xmwjtest.oss-cn-shenzhen.aliyuncs.com/MagicYinSound_o7RSnwtOHaFeDLvEu14UVV-zeegA_1497085237000.m4a";
        String name = "普通";
        SwitchVideoModel switchVideoModel = new SwitchVideoModel(name, source1);

        //视频1
        String source2 = "http://xmwjtest.oss-cn-shenzhen.aliyuncs.com/MagicYinSound_o7RSnwtOHaFeDLvEu14UVV-zeegA_1497085237000.m4a";
        String name2 = "清晰";
        SwitchVideoModel switchVideoModel2 = new SwitchVideoModel(name2, source2);

        //添加到List中
        List<SwitchVideoModel> list = new ArrayList<>();
        list.add(switchVideoModel);
        list.add(switchVideoModel2);

        videoPlayer.setUp(list, true, "");

        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放全部
        videoPlayer.setStandardVideoAllCallBack(null);
        GSYVideoPlayer.releaseAllVideos();
    }
}
